<?php

/**
 * marque actions.
 *
 * @package    emc
 * @subpackage marque
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class marqueActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }


  public function executeList(sfWebRequest $request)
  {
    $this->notify = $request->getParameter('notify', null);
  	$jobboard_id = $request->getParameter('jobboard_id', null);
    if ($jobboard_id) {
      $this->jobboard = Doctrine_Core::getTable('Jobboard')->find($jobboard_id);
      if ($this->jobboard) {
        $marque_list = Doctrine_Core::getTable('Marque')->findByJobboardIdAndIsActive($jobboard_id, true);
      } else {
        $this->notify = "jobboard_not_found";
      }
    } else
      $marque_list = Doctrine_Core::getTable('Marque')->findByIsActive(true);
    $q = Doctrine_Query::create()
              ->from('MarqueRestriction mr')
              ->select('mr.name');
    if ($jobboard_id)
      $q->addWhere('mr.jobboard_id = ?', $jobboard_id);
    
    $marques_restricted_list = $q->fetchArray();
    $marques_restricted = array();
    foreach ($marques_restricted_list as $marque)
      $marques_restricted[] = $marque['name'];
    $this->marques = array();
    foreach ($marque_list as $marque)
      if (!in_array($marque->name, $marques_restricted))
        $this->marques[] = $marque;
    return sfView::SUCCESS;
  }

  
  public function executeCatalogList(sfWebRequest $request)
  {
    $marque_id = $request->getParameter('marque_id', null);
    $q = Doctrine_Query::create()
          ->from('Marque m')
          ->where('m.id LIKE ?', $marque_id);
    foreach ($q->execute() as $result) {
      $this->marque = $result;
      break;
    }

    return sfView::SUCCESS;
  }

  
  public function executeBrandList(sfWebRequest $request)
  {
    $catalog_id = $request->getParameter('catalog_id', null);
    $this->catalog = Doctrine_Core::getTable('Catalog')->find($catalog_id);
    
    return sfView::SUCCESS;
  }


  public function executeEdit(sfWebRequest $request)
  {
    $data = $request->getParameter('marque', null);
    if ($data) {
      $marque = Doctrine_Core::getTable('Marque')->find($data['marque_id']);
      try {
        $marque->name = $data['name'];
        $marque->jobboard_id = $data['jobboard_id'];
        $marque->description = $data['description'];
        $marque->little_logo = $data['little_logo'];
        $marque->logo = $data['logo'];
        $marque->cover_picture = $data['cover_picture'];
        $marque->href = $data['href'];
        $marque->is_full_ = isset($data['is_full_import']) ? true : false;
        $marque->is_active = isset($data['is_active']) ? true : false;
        $marque->save();
        $this->redirect('@marque_list?notify=update_marque_success');
      } catch (Exception $e) {
        $this->notify = "update_marque_failed";
      }
    } else {
        $this->marque_id = $request->getParameter('marque_id', null);
        if ($this->marque_id) { // it's an edit action
          $marque = Doctrine_Core::getTable('Marque')->find($this->marque_id);
          if (isset($marque) && $marque)
            $this->marque_form = new MarqueForm($marque);
          else {
            $this->redirect('@marque_list?notify=marque_not_found');
          }
        } else {
          $this->redirect('@marque_list?notify=marque_not_found');
        }
      }
    return sfView::SUCCESS;
  }

  public function executeSwitchToRestriction(sfWebRequest $request)
  {
    $marque_id = $request->getParameter('marque_id');
    $marque = Doctrine_Core::getTable('Marque')->find($marque_id);
    if (isset($marque)) {
      $marque_restriction = Doctrine_Core::getTable('MarqueRestriction')->findOneByJobboardIdAndName($marque->jobboard_id, $marque->name);
      if (!$marque_restriction) {
        $marque_restriction = new MarqueRestriction();
        $marque_restriction->jobboard_id = $marque->jobboard_id;
        $marque_restriction->name = $marque->name;
        $marque_restriction->save();
        $this->getRequest()->setParameter('jobboard_id', $marque->jobboard_id);
        $notify = 'marque_restricted_success';

        // deactivate all associated categories and products
        $category = Doctrine_Core::getTable('Category')->find($marque->category_id);
        $category->deactivate();
      } else 
        $notify = "marque_restriction_exists";
    } else {
      $notify = "marque_not_found";
    }
    $this->redirect('@marque_list?jobboard_id='.$marque->jobboard_id.'&notify='.$notify);
  }
}


